package com.qingke.easyjava.besuper2.command.impi;

import java.util.List;

import com.qingke.easyjava.besuper2.AnswerDao;
import com.qingke.easyjava.besuper2.QuestionDao;
import com.qingke.easyjava.besuper2.command.PlayerCommand;
import com.qingke.easyjava.besuper2.pojo.Answer;
import com.qingke.easyjava.besuper2.pojo.Player;
import com.qingke.easyjava.besuper2.pojo.Question;
import com.qingke.easyjava.utils.QingkeConsole;

public class AnswerCommand extends PlayerCommand {

	public AnswerCommand(Player playerProfile) {
		super(playerProfile);
	}

	@Override
	public void execute() {
		int qId = QingkeConsole.askUserInputInt("Please enter question id");
		
		QuestionDao qd = new QuestionDao();
		Question q = qd.getQuestion(qId);
		
		if(q == null) {
			QingkeConsole.println("Invalid question id!");
			return;
		}
		
		if(q.getPlayerId() == playerProfile.getId()) {
			QingkeConsole.println("You are not allowed to your question!");
			return;
		}
		
		if(q.isSolved()) {
			QingkeConsole.println("The question has been solved!");
			return;
		}
		
		String content = QingkeConsole.askUserInputString("Please enter your answer");
		
		AnswerDao ad = new AnswerDao();
		Answer a = new Answer(q.getId(), content, playerProfile.getId());
		ad.upsertAnswer(a);
		
		List<Answer> aList = ad.getAnswersFrom(playerProfile.getId());
		playerProfile.setAnswers(aList);
	}

}
